package com.jinzhi.jzweb.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jinzhi.common.base.CoreServiceImpl;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.dao.ChapterDocumentDao;
import com.jinzhi.jzweb.dao.CoursesDao;
import com.jinzhi.jzweb.domain.CoursesDO;
import com.jinzhi.jzweb.pojo.RecordsVo;
import com.jinzhi.jzweb.pojo.entity.ChapterDocument;
import com.jinzhi.jzweb.pojo.entity.Records;
import com.jinzhi.jzweb.service.ChapterDocumentService;
import com.jinzhi.jzweb.service.CoursesService;
import com.jinzhi.jzweb.service.RecordsService;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/*
 *  @author : JingChangTian
 *  @date   : 2023/12/11 14:28
 *  @description :
 *
 */
@Service
public class ChapterDocumentServiceImpl extends CoreServiceImpl<ChapterDocumentDao, ChapterDocument> implements ChapterDocumentService {

    private final CoursesDao coursesDao;

    private final UserService userService;

    private final RecordsService recordsService;

    public ChapterDocumentServiceImpl(CoursesDao coursesDao, UserService userService, RecordsService recordsService) {
        this.coursesDao = coursesDao;
        this.userService = userService;
        this.recordsService = recordsService;
    }

    @Override
    public Result getConceptType() {
        RecordsVo recordsVo = new RecordsVo();
        List<ChapterDocument> documents = this.selectList(new EntityWrapper<ChapterDocument>());
        if(documents.isEmpty()){
            return Result.ok();
        }
        //概览数据
        Map<Integer, Long> groupByCourse = documents.stream().collect(Collectors.groupingBy(ChapterDocument::getType,Collectors.counting()));
        //用户上传统计
        List<CoursesDO> coursesDOS = coursesDao.selectList(new EntityWrapper<CoursesDO>());
        List<UserDO> users = userService.selectList(new EntityWrapper<UserDO>());
        Map<String, Long> groupByUser = coursesDOS.stream().collect(Collectors.groupingBy(x -> Optional.ofNullable(x.getCreaterName()).orElse("-"),Collectors.counting()));
        //点击下载次数统计
        List<Records> records = recordsService.selectList(new EntityWrapper<Records>());
        //点击次数
        List<Records> tap = records.stream().filter(a -> a.getMovement() == 0).collect(Collectors.toList());
        //下载次数
        List<Records> download = records.stream().filter(a -> a.getMovement() == 1).collect(Collectors.toList());

        recordsVo.setGroupByCourse(groupByCourse);
        recordsVo.setGroupByUser(groupByUser);
        recordsVo.setDownload(download);
        recordsVo.setTap(tap);
        return Result.ok(recordsVo);
    }

    @Override
    public Result tapUpdate(Records records) {
        Records rec = recordsService.selectOne(records);
        rec.setFrequency(rec.getFrequency()+1);
        return recordsService.updateById(rec)? Result.ok() : Result.fail();
    }

    @Override
    public int updatePdfById(String resourceId, String pdfUrl) {
        return this.baseMapper.updatePdfById(resourceId,pdfUrl);
    }
}
